CMU 15-112 Summer 2019: Fundamentals of Programming and Computer Science
Homework 4 (Due Wed 29-May, at 5pm)




  1. Attend a small group [5pts]
    By quiz time (Friday 5:30pm), attend a small group session hosted by one of our TAs. Note that this does not need to be done by the homework deadline - you can attend any session as long as it is before the quiz on Friday.

  2. lookAndSay(a) [35pts]
    First, read about look-and-say numbers here. Then, write the function lookAndSay(a) that takes a list of numbers and returns a list of numbers that results from "reading off" the initial list using the look-and-say method, using tuples for each (count, value) pair. For example:
      lookAndSay([]) == []
      lookAndSay([1,1,1]) == [(3,1)]
      lookAndSay([-1,2,7]) == [(1,-1),(1,2),(1,7)]
      lookAndSay([3,3,8,-10,-10,-10]) == [(2,3),(1,8),(3,-10)]
    
    Hint: you'll want to keep track of the current number and how many times it has been seen.

  3. nondestructiveRotateList(a, n) [30pts]
    Write the function nondestructiveRotateList(a, n) which takes a list a and an integer n, and nondestructively modifies the list so that each element is shifted to the right by n indices (including wraparound). The function should then return this new list. For example:
        nondestructiveRotateList([1,2,3,4], 1) -> [4,1,2,3]
        nondestructiveRotateList([4,3,2,6,5], 2) -> [6, 5, 4, 3, 2]
        nondestructiveRotateList([1,2,3], 0) -> [1,2,3]
        nondestructiveRotateList([1, 2, 3], -1) -> [2, 3, 1]
    

  4. destructiveRotateList(a, n) [30pts]
    This function works the same as the previous function, only here it is destructive. That is, it directly changes the list a, so after the call, that exact list is rotated n indices to the right with wraparound, and a new list is not created. As usual for destructive functions, this function returns None. Also: you may not call the nondestructive version here, and in fact, you may not even create a new list (or tuple or other similar data structure) that is longer than 10 elements (you must modify the original list in place).
    Hint: the destructive list methods will be very helpful for this problem.